﻿/**
 * @author sminutoli
 * version 1.0
 * Tengo el problema del onLoad(), ya que se dispara después del drawItem(), por lo que le disparo el onLoad() desde afuera
 * Estoy probando el onLoad() en el constructor del clip
 * Hay una variable en checkScroll(), step:Number, que hay que revisar para qué la puse
 */
 
import com.dinmax.flash.draw.cabeza.*
import com.dinmax.flash.utilities.movieclip.MovieClipUtils
import com.dinmax.flash.draw.layout.*

class WindowCabeza
extends ControlCabeza
implements SelectedListener, ChangedListener, LayoutCapable {
	
	private var mcMask:MovieClip;
	private var scrollV:ScrollCabeza;
	private var scrollH:ScrollCabeza;
	
	private var _layout:Layout;
	private var contentClip:MovieClip;
	private var items:Array; //de MovieClip;
		
	public function WindowCabeza() {
		//trace("\n---\nnew WindowCabeza()");
		//this.onLoad();
	}
	
	function onLoad(){
		//trace("WindowCabeza.as | onLoad()");
		super.onLoad();
		this.init();
				
	};
		
	function init():Void {
		//trace("WindowCabeza.as init()");
		items = new Array();
				
		//MovieClipUtils.destroyClip(this.contentClip);				
		if(this.contentClip == undefined) {
			//trace("WindowCabeza.as init() contentClip= undefined creando");
			this.contentClip = this.createEmptyMovieClip("content", this.getNextHighestDepth());
			this.contentClip.setMask(this.mcMask);
			//trace("WindowCabeza.as init() contentClip= "+this.contentClip);
		} else {
			//trace("WindowCabeza.as init() el content clip ya existe");
		};
		return;
	};
	
	public function initChild(control:ControlCabeza):Void {
		switch(control){
			case this.scrollV:
			this.initScrollV();
			break;
			
			case this.scrollH:
			this.initScrollH();
			break;
			
			case this.contentClip:
			this.contentClip.setMask(this.mcMask);
			this.contentClip.setListener(this);
			break;
		};	
	};
		
	private function initScrollV():Void{
		//trace("WindowCabeza.as | init() | scrollV.init()");
		//trace("WindowCabeza.as | initScrollV");
		scrollV.init();
		scrollV.setStep(5);
		scrollV.setScrollPos(0);
		scrollV.addListener(this);
		this.checkScroll();
		return;
	};
	
	private function initScrollH():Void{
		//trace("WindowCabeza.as | initScrollH");
		scrollH.init();
		scrollH.setStep(5);
		scrollH.setScrollPos(0);
		scrollH.addListener(this);
		this.checkScroll();
		return;
	};
	
	// DRAW ITEMS
	
	public function drawItem(linkId:String):MovieClip {
		var name:String = MovieClipUtils.getSerialName(this.contentClip, linkId);
		var clip:MovieClip = this.contentClip.attachMovie(linkId, name, this.contentClip.getNextHighestDepth());
		//trace("WindowCabeza.as | drawItem() linkId= "+linkId+" name= "+name+" contentClip= "+contentClip);
		if(!clip) {
			throw new Error("\n---\nWindowCabeza.as\nERROR= LINKAGE ID INEXISTENTE= "+linkId+"\n---");
		};
		this.items.push(clip);
		this.layout();
		this.checkScroll();
		return clip;
	};
	
	public function deleteItems():Void {
		//trace("WindowCabeza.as | deleteItems()");
		for (var i : Number = 0; i < this.items.length; i++) {
			MovieClipUtils.destroyClip(this.items[i]);
			//removeMovieClip(this.items[i]);
		}
		this.items = new Array();
		return;
	};
	
	public function getItems():Array {
		return items;
	}

	public function setItems(items:Array):Void {
		this.items = items;
		return;
	}
	
	//
	
	//SELECTED LISTENER
	public function selected(control:ControlCabeza):Void{
		switch(control){
			case this.scrollH:
			this.contentClip._x = this.mcMask._x - ScrollCabeza(control).getScrollPos() * (this.contentClip._width - this.mcMask._width) / 100;
			break;
			
			case this.scrollV:
			this.contentClip._y = this.mcMask._y - ScrollCabeza(control).getScrollPos() * (this.contentClip._height - this.mcMask._height) / 100;
			break;
		};
	};
	//
	
	//CHANGED LISTENER
	public function changed(control:ControlCabeza, type:String):Void {
		if(control == this.contentClip) this.checkScroll();
		return;
	}
	//
	
	//LAYOUT CAPABLE
	public function layout():Void {
		this.getLayout().layout();
	};
	public function setLayout(layout:Layout):Void {
		this._layout = layout;
	};
	public function getLayout():Layout{
		return this._layout;
	};
	//
	
	private function checkScroll():Boolean {
		var difH:Number = this.contentClip._width - this.mcMask._width;
		var difV:Number = this.contentClip._height - this.mcMask._height;
		var check:Boolean = false;
		//trace("WindowCabeza.as | checkScroll difH= "+difH+" difV= "+difV);
		
		if(difH > 0){
			this.scrollH.setEnabled();
			check = true;
		} else {
			this.scrollH.setDisabled();
		};
		
		if(difV > 0){
			var step:Number = 100 - Math.round(difV * 100 / this.mcMask._height);
			//this.scrollV.setStep(step);
			this.scrollV.setEnabled();
			check = true;
			trace("WindowCabeza | checkScroll | scrollV enabled");
		} else {
			this.scrollV.setDisabled();
			trace("WindowCabeza | checkScroll | scrollV disabled");
		};
		
		return check;
	};
		
	public function getScrollV():ScrollCabeza {
		trace("getScrollV | "+scrollV);
		return scrollV;
	}

	public function setScrollV(scrollV:ScrollCabeza):Void {
		this.scrollV = scrollV;
	}

	public function getScrollH():ScrollCabeza {
		return scrollH;
	}

	public function setScrollH(scrollH:ScrollCabeza):Void {
		this.scrollH = scrollH;
	}

}